package com.zx.controller.order;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.zx.config.jackson.JacksonObjectMapper;
import com.zx.dao.goods.GoodsImgMapper;
import com.zx.dto.request.order.OrderAddDtoReq;
import com.zx.dto.request.order.OrderDtoRequest;
import com.zx.dto.response.goods.GoodsImgDto;
import com.zx.dto.response.order.OrderDetailsDtoResult;
import com.zx.dto.response.order.OrderDetailsList;
import com.zx.enums.GoodsImgTypeEnums;
import com.zx.service.goods.GoodsImgService;
import com.zx.service.order.OrderDetailsService;
import com.zx.service.order.OrderService;
import com.zx.service.order.StateService;
import com.zx.service.shop.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author sunkai
 * @date 2022/8/21 0021 18:17
 * @mood zhou xun
 */
@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailsService orderDetailsService;

    @PostMapping("/add/order")
    @ResponseBody
    public boolean addOrder(@CookieValue(value = "UserToken") String userToken, @RequestParam(value = "jsonStr") String jsonStr) {
        OrderAddDtoReq orderAddDtoReqs = JSON.parseObject(jsonStr, OrderAddDtoReq.class);
        return orderService.addOrder(orderAddDtoReqs, userToken);
    }

    @GetMapping("/details/list")
    @ResponseBody
    public OrderDetailsList queryOrderDetails(OrderDtoRequest orderDtoRequest, @CookieValue("UserToken") String userToken) {
        return orderDetailsService.queryOrderDetails(orderDtoRequest, userToken);
    }

    @PostMapping("/update/state")
    @ResponseBody
    public Boolean updateOrderState(OrderDtoRequest orderDtoRequest, @CookieValue("UserToken") String userToken) {
        return orderService.updateOrderDetails(userToken, orderDtoRequest);
    }

    @GetMapping("/details")
    public String OrderDetails(@RequestParam(value = "orderId") String order, @CookieValue("UserToken") String userToken, Model model) {
        OrderDtoRequest orderDtoRequest = new OrderDtoRequest();
        orderDtoRequest.setOrderId(Integer.valueOf(order));
        OrderDetailsList orderDetailsList = orderDetailsService.queryOrderDetails(orderDtoRequest, userToken);
        String s = JSON.toJSONString(orderDetailsList);
        model.addAttribute("orderDetailsList", s);
        return "home-orderDetail";
    }
}
